﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
//using MQ.Core.Admin.Schedule;
//using Quartz;
//using Quartz.Impl;
//using Quartz.Spi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MQ.Core.Admin.Extensions.Quartz
{
    public static class QuartzExtension
    {
        public static IServiceCollection AddQuartz(this IServiceCollection services,Type jobType)
        {
            #region Configure Quartz DI

            //services.Add(new ServiceDescriptor(typeof(IJob), jobType, ServiceLifetime.Transient));
            //services.AddSingleton<IJobFactory, ScheduledJobFactory>();
            //services.AddSingleton<IJobDetail>(provider =>
            //{
            //    return JobBuilder.Create<ScheduledJob>()
            //      .WithIdentity("Sample.job", "group1")
            //      .Build();
            //});

            //services.AddSingleton<ITrigger>(provider =>
            //{
            //    return TriggerBuilder.Create()
            //    .WithIdentity($"Sample.trigger", "group1")
            //    .StartNow()
            //    .WithSimpleSchedule
            //     (s =>
            //        s.WithInterval(new TimeSpan(0, 0, 5))
            //        .RepeatForever()
            //     )
            //     .Build();
            //});

            //services.AddSingleton<IScheduler>(provider =>
            //{
            //    var schedulerFactory = new StdSchedulerFactory();
            //    var scheduler = schedulerFactory.GetScheduler().Result;
            //    scheduler.JobFactory = provider.GetService<IJobFactory>();
            //    scheduler.Start();
            //    return scheduler;
            //});

            #endregion
            return services;
        }

        public static void UseQuartz(this IApplicationBuilder app)
        {
            //app.ApplicationServices.GetService<IScheduler>()
            //    .ScheduleJob(app.ApplicationServices.GetService<IJobDetail>(),
            //    app.ApplicationServices.GetService<ITrigger>()
            //    );
        }
    }
}
